#include "gd32h7xx.h"
#include "bsp_mpu.h"

#define AXI_SRAM_ADDR 0x24000000

extern uint32_t Image$$RW_ITCM$$Base;
extern uint32_t Image$$RW_ITCM$$Limit[];
extern uint32_t Image$$RW_DTCM$$Base;
extern uint32_t Image$$RW_DTCM$$Limit[];
extern uint32_t Image$$RW_AXI$$Base;
extern uint32_t Image$$RW_AXI$$Limit[];


void bsp_mpu_config(void)
{
    mpu_region_init_struct mpu_init_struct;
    mpu_region_struct_para_init(&mpu_init_struct);

    /* disable the MPU */
    ARM_MPU_Disable();
    ARM_MPU_SetRegion(0, 0);

    /* configure the MPU attributes for AXI SRAM */
    mpu_init_struct.region_base_address  = Image$$RW_AXI$$Base;
    mpu_init_struct.region_size          = MPU_REGION_SIZE_512KB;
    mpu_init_struct.access_permission    = MPU_AP_FULL_ACCESS;
    mpu_init_struct.access_bufferable    = MPU_ACCESS_BUFFERABLE;
    mpu_init_struct.access_cacheable     = MPU_ACCESS_CACHEABLE;
    mpu_init_struct.access_shareable     = MPU_ACCESS_NON_SHAREABLE;
    mpu_init_struct.region_number        = MPU_REGION_NUMBER0;
    mpu_init_struct.subregion_disable    = MPU_SUBREGION_ENABLE;
    mpu_init_struct.instruction_exec     = MPU_INSTRUCTION_EXEC_PERMIT;
    mpu_init_struct.tex_type             = MPU_TEX_TYPE0;
    mpu_region_config(&mpu_init_struct);
    mpu_region_enable();

    /* enable the MPU */
    ARM_MPU_Enable(MPU_MODE_PRIV_DEFAULT);
}